// 剖面分析

export const BQGIS_sectionMeasure = {
    /**
     *
     * 初始化剖面分析
     *
     * @param  viewer           Mars3d实例对象
     * @param  measureStart     回调函数(绘制完成，开始分析时触发) 
     * @param  measureEnd       回调函数(分析完成时触发,返回分析数据)
     */
    init({
        viewer = window.viewer,
        measureStart,
        measureEnd,
    }) {
        if (!viewer.bq_sectionMeasure) {
            viewer.bq_sectionMeasure = new mars3d.thing.Measure({
                // 设置文本样式
                label: {
                    color: "#ffffff",
                    font_family: "楷体",
                    font_size: 20
                }
            })
            viewer.addThing(viewer.bq_sectionMeasure)
        }
        viewer.bq_sectionMeasure.off()
        // 触发事件：开始分析前
        viewer.bq_sectionMeasure.on(mars3d.EventType.start, function (e) {
            measureStart(e)
        })

        // 触发事件：异步分析完成后
        viewer.bq_sectionMeasure.on(mars3d.EventType.end, function (e) {
            if (e.graphic ?.type === mars3d.graphic.SectionMeasure.type) {
                measureEnd(e)
            }
        })

    },
    /**
     *
     * 开始绘制
     *
     * @param  option           参考 mars3d 的 Measure.section 函数参数
     * @param  viewer           Mars3d实例对象
     * 
     * @return  绘制的 SectionMeasure 对象，参考 mars3d 的 SectionMeasure
     */
    start(option = {
        splitNum: 300
    },viewer = window.viewer) {
        return viewer.bq_sectionMeasure.section(option)
    },
    // 清除 top 弹窗
    hideTipMarker(viewer = window.viewer) {
        console.log(viewer.bq_tipSectionGraphic,'viewer.bq_tipSectionGraphic')
        if (!viewer.bq_tipSectionGraphic) {
            return
        }
        viewer.bq_tipSectionGraphic.remove(true)
        viewer.bq_tipSectionGraphic = null
    },
    /**
     *
     * 显示 top 弹窗
     *
     * @param  point         弹窗坐标[经度，纬度，高度]
     * @param  inthtml       回调函数(分析完成时触发，返回)
     */
    showTipMarker(point, inthtml, viewer = window.viewer) {
        const position  = Cesium.Cartesian3.fromDegrees(...point)
        if (!viewer.bq_tipSectionGraphic) {
            viewer.bq_tipSectionGraphic = new mars3d.graphic.PointEntity({
                name: "当前点",
                position: position ,
                style: {
                    color: "#0000ff",
                    pixelSize: 7,
                    outlineColor: "#ffffff",
                    outlineWidth: 2,
                }
            }).addTo(viewer.graphicLayer)
        }
        viewer.bq_tipSectionGraphic.position  = position 
        viewer.bq_tipSectionGraphic.bindPopup(inthtml).openPopup()
    },
    // 清除所有标识
    clearAll(viewer = window.viewer){
        viewer.bq_sectionMeasure.clear()
        this.hideTipMarker(viewer)
    }
}